package com.wy.order.controller;


import com.wy.order.VO.ProductInfoVO;
import com.wy.order.VO.ProductVO;
import com.wy.order.VO.ResultVO;
import com.wy.order.dataobject.ProductCategory;
import com.wy.order.dataobject.ProductInfo;
import com.wy.order.service.ProductCategoryService;
import com.wy.order.service.ProductInfoService;
import com.wy.order.utils.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductCategoryService productCategoryService;

    @GetMapping("/list")
    public ResultVO list(){

        List<ProductInfo> productInfoList = productInfoService.findUpAll();

        List<Integer> categoryList = new ArrayList<>();
        for (ProductInfo p :productInfoList) {
            categoryList.add(p.getCategoryType());
        }

        List<ProductCategory> productCategoryList = productCategoryService.findByCategoryTypeIn(categoryList);


        List<ProductVO> productVOList = new ArrayList<>();

        for (ProductCategory category:productCategoryList) {

            ProductVO productVO = new ProductVO();
            productVO.setCategoryName(category.getCategoryName());
            productVO.setCategoryType(category.getCategoryType());

            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for (ProductInfo info:productInfoList) {

                if (info.getCategoryType().equals(category.getCategoryType())){

                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(info,productInfoVO);

                    productInfoVOList.add(productInfoVO);
                }
            }


            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }


//        ResultVO<List<ProductVO>> resultVO = new ResultVO<>();
//        resultVO.setMsg("cg");
//
//        resultVO.setData(productVOList);
        return ResultVOUtil.success(productVOList);
    }
}
